package com.jueban.controller;

import com.jueban.Dto.ApplyRewardDto;
import com.jueban.Dto.ApplyRewardQueryDto;
import com.jueban.Dto.SuggestAuditDto;
import com.jueban.Vo.ApplyRewardVo;
import com.jueban.Vo.ApplyRewardsVo;
import com.jueban.Vo.ResultVo;
import com.jueban.model.ApplyReward;
import com.jueban.service.ApplyRewardService;
import com.jueban.util.Messages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/reward")
public class ApplyRewardController {

    private final ApplyRewardService applyRewardService;

    @Autowired
    public ApplyRewardController(ApplyRewardService applyRewardService) {
        this.applyRewardService = applyRewardService;
    }

    // 申请奖励
    @PreAuthorize("isAuthenticated()")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVo applyReward(@RequestBody @Valid ApplyRewardDto rewardDto) {
        return applyRewardService.applyReward(rewardDto);
    }

    // 查询奖励申请
    @PreAuthorize("isAuthenticated()")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ApplyRewardVo getApplyReward(@PathVariable("id") int id) {
        return applyRewardService.getApplyReward(id);
    }

    // 查看奖励申请
    @RequestMapping(value = "/rewards", method = RequestMethod.POST)
    public ApplyRewardsVo getApplyRewards(@RequestBody @Valid ApplyRewardQueryDto applyRewardQueryDto) {
        ApplyRewardsVo applyRewardsVo = new ApplyRewardsVo();
        Page<ApplyReward> page = null;
        String queryType = applyRewardQueryDto.getQueryType();

        if (queryType.equals("self")) {
            page = applyRewardService.findSelfApplyReward(applyRewardQueryDto);
        } else if (queryType.equals("admin")) {
            page = applyRewardService.findAdminApplyRewards(applyRewardQueryDto);
        } else if (queryType.equals("teacher")) {
            page = applyRewardService.findTeacherApplyRewards(applyRewardQueryDto);
        }

        applyRewardsVo.setPreviewProperties(page);
        applyRewardsVo.setSucceeded();
        applyRewardsVo.setMessages(Messages.APPLY_REWARD_FIND_SUCCEED);

        return applyRewardsVo;
    }

    // 审核奖励
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResultVo auditReward(@RequestBody @Valid SuggestAuditDto suggestAuditDto) {
        return applyRewardService.auditReward(suggestAuditDto);
    }


}
